package itsm.isperp.module.service.db;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.db.DbDataBackupLog;
import itsm.isperp.module.repository.db.DbDataBackupLogMapper;

import java.io.File;
import java.sql.Timestamp;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DbDataBackupLogService extends BaseService<DbDataBackupLog> {

	@Autowired
	protected DbDataBackupLogMapper dbDataBackupLogMapper;

	@Override
	public DbDataBackupLogMapper getRepository() {
		return dbDataBackupLogMapper;
	}

	@Override
	public ResultMessage delete(String id) {
		DbDataBackupLog log = this.getRepository().findOne(id);
		File f = new File(ContextHolder.getApplicationPath() + File.separator
				+ log.getFilePath());
		if (f.exists()) {
			f.delete();
		}

		this.getRepository().deleteByClass(id, DbDataBackupLog.class);
		return ResultMessage.success();

	}

	public int save(String fileName, String filePath, int fileSize) {
		DbDataBackupLog log = new DbDataBackupLog();
		log.setFileName(fileName);
		log.setFilePath(filePath);
		log.setFileSize(fileSize);
		log.setCreateDate(new Timestamp(new Date().getTime()));
		return this.saveOrUpdate(log);

	}
}
